package com.example.gsq.news.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.gsq.news.util.C;

public class BaseHandler extends Handler {

    private BaseActivity mActivity;

    private BaseFragment mFragment;

    public BaseHandler(BaseActivity activity) {
        mActivity = activity;
    }

    public BaseHandler(BaseFragment fragment) {
        mFragment = fragment;
    }

    @Override
    public void handleMessage(Message msg) {
        Bundle bundle = msg.getData();
        int taskId = bundle.getInt("TaskId");
        int taskType = bundle.getInt("TaskType");
        switch (msg.what) {
            case C.TaskStatus.TASK_COMPLETE:
                if (taskId == C.TaskId.ACTIVITY_GET_JSON_LIST) {
                    String response = bundle.getString("response");
                    mActivity.onTaskComplete(taskId, taskType, response);
                } else if (taskId == C.TaskId.ACTIVITY_GET_ITEM_BITMAP) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    String imageUrl = bundle.getString("imageUrl");
                    mActivity.onTaskComplete(taskId, taskType, bitmap, imageUrl);
                } else if (taskId == C.TaskId.FRAGMENT_GET_JSON_LIST) {
                    String response = bundle.getString("response");
                    mFragment.onTaskComplete(taskId, taskType, response);
                } else if (taskId == C.TaskId.FRAGMENT_GET_ITEM_BITMAP) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    String imageUrl = bundle.getString("imageUrl");
                    mFragment.onTaskComplete(taskId, taskType, bitmap, imageUrl);
                }
                break;
            default:
                break;
        }
    }
}
